package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.annotation.AutoFill;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.DishFlavorsMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.result.PageResult;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
public class DishServiceImpl implements DishService {

    @Autowired
    private DishMapper dishMapper;

    @Autowired
    private DishFlavorsMapper dishFlavorsMapper;

    @Autowired
    private SetmealDishMapper setmealDishMapper;

    /**
     * 新增菜品
     *
     * @param dishDTO
     */
    @Transactional
    @Override
    public void saveDish(DishDTO dishDTO) {
        //创建一个菜品类对象
        Dish dish = new Dish();
        //将dishDTO拷贝到菜品类对象当中
        BeanUtils.copyProperties(dishDTO, dish);
        dishMapper.insert(dish);
        //获取insert返回的菜品id
        Long dishId = dish.getId();
        List<DishFlavor> flavors = dishDTO.getFlavors();
        if (flavors != null && !flavors.isEmpty()) {
            flavors.forEach(s -> {
                s.setDishId(dishId);
            });
            dishFlavorsMapper.insert(flavors);
        }
    }

    /**
     * 菜品分页查询
     *
     * @param dishPageQueryDTO
     * @return
     */
    @Override
    public PageResult page(DishPageQueryDTO dishPageQueryDTO) {
        PageHelper.startPage(dishPageQueryDTO.getPage(), dishPageQueryDTO.getPageSize());
        Page<DishVO> page = dishMapper.page(dishPageQueryDTO);
        long total = page.getTotal();
        List<DishVO> result = page.getResult();
        return new PageResult(total, result);
    }

    /**
     * 批量删除菜品
     * @param ids
     */
    @Transactional
    @Override
    public void deleteDish(List<Long> ids) {
        //判断当前菜品的状态，如果是起售状态则不能删除，如果是停售则可以删除
        for (Long id : ids) {
           Dish dish = dishMapper.getById(id);
           if(dish.getStatus() == StatusConstant.ENABLE){
               throw new DeletionNotAllowedException(MessageConstant.DISH_ON_SALE);
           }
        }
        //判断当前菜品是否关联套餐
        List<Long> idList = setmealDishMapper.getSetmaelIdsByDishIds(ids);
        if(idList != null && !idList.isEmpty()){
            throw new DeletionNotAllowedException(MessageConstant.DISH_BE_RELATED_BY_SETMEAL);
        }
        //删除菜品表中的菜品数据
        /*for (Long id : ids) {
            dishMapper.deleteById(id);
            //删除菜品关联的口味数据
            dishFlavorsMapper.deleteByDishId(id);
        }*/
        //根据id批量删除菜品
        dishMapper.deleteByIds(ids);
        //删除菜品关联的口味数据
        dishFlavorsMapper.deleteByDishIds(ids);
    }

    @Override
    public DishVO saveById(Long id) {
        //根据id查询菜品表中的数据
       Dish dish = dishMapper.selectById(id);
       //根据id查询口味表中的数据
       List<DishFlavor> flavors =  dishFlavorsMapper.selectByDishId(id);

       DishVO dishVO = new DishVO();
       BeanUtils.copyProperties(dish,dishVO);
       dishVO.setFlavors(flavors);
        return dishVO;
    }

    @Override
    public void update(DishDTO dishDTO) {
        //修改菜品基本信息
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO,dish);
        dishMapper.update(dish);
        //删除原有的口味信息
        dishFlavorsMapper.deleteByDishId(dishDTO.getId());
        //插入口味信息
        List<DishFlavor> flavors = dishDTO.getFlavors();
        if (flavors != null && !flavors.isEmpty()) {
            flavors.forEach(s -> {
                s.setDishId(dishDTO.getId());
            });
            dishFlavorsMapper.insert(flavors);
        }
    }

    /**
     * 根据分类id查询菜品
     * @param dish
     * @return
     */
    @Override
    public List<DishVO> saveList(Dish dish) {
        List<DishVO> dishVOList = new ArrayList<>();

        List<Dish> dishList = dishMapper.list(dish);

        for (Dish d : dishList) {
            DishVO dishVO = new DishVO();

            BeanUtils.copyProperties(d,dishVO);

            List<DishFlavor> flavors = dishFlavorsMapper.selectByDishId(d.getId());

            dishVO.setFlavors(flavors);

            dishVOList.add(dishVO);
        }

        return dishVOList;
    }

    /**
     * 修改菜品状态
     * @param status
     * @param id
     */
    @Override
    public void setStatus(Integer status, Long id) {
        Dish dish = Dish.builder().id(id).status(status).build();
        dishMapper.update(dish);

    }
}
